{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "08069983-9e19-486c-826e-b29bfc56111c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "cb52dd7a-2180-4a04-a0a2-f4fc9af32158",
   "metadata": {},
   "outputs": [],
   "source": [
    "# dir(np)\n",
    "# help(np.gcd)\n",
    "# shift tab - 查看方法参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14bcd402-062a-4987-9750-50ca1c8617c2",
   "metadata": {},
   "source": [
    "1. 从 list 引出 numpy 数组\n",
    "2. 创建数组 api\n",
    "3. 查看数组\n",
    "4. 保存和加载数组\n",
    "5. 数据类型与转换\n",
    "6. 数组运算\n",
    "7. 数组拷贝\n",
    "8. 索引、切片\n",
    "9. 形状改变\n",
    "10. 广播机制\n",
    "11. 通用函数\n",
    "12. 矩阵运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ce9c8e5-f4c1-4e21-9e2c-87e9ce4714e1",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## 引出 numpy 数组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2277a57b-7526-4f03-b764-4230c305be3f",
   "metadata": {},
   "source": [
    "**回顾 list**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ca1cbcf3-037f-464d-8f73-6d35aba8e75e",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst = [0, 1, 2, 3, 4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9ad8d800-8a73-4326-8f72-dc077a2acaf3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 3 4\n",
      "[1, 2]\n"
     ]
    }
   ],
   "source": [
    "print(lst[0], lst[3], lst[-1])\n",
    "print(lst[1:3]) # 切片，左开右闭\n",
    "# 无法一次取出多个"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1de6ab01-2bfb-4e7c-a1d1-0f8340e0c183",
   "metadata": {},
   "source": [
    "**根据 list 对象创建 numpy 数组**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ea005da2-cfa4-4976-b99d-448a35bee356",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.array(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "71cd72af-2eb7-4128-8c31-dafc70dbca06",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 3 4\n",
      "[1 2]\n"
     ]
    }
   ],
   "source": [
    "print(arr[0], arr[3], arr[-1])\n",
    "print(arr[1:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6b83f797-dc74-4525-8a8c-2f9d2625afe6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 4]\n"
     ]
    }
   ],
   "source": [
    "print(arr[[2,4]]) # 一次取出多个"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82f58b17-dec0-4464-bf64-f5de573a0df6",
   "metadata": {},
   "source": [
    "*numpy 数组可以做筛选*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3685a262-8be3-4075-9a7f-37e3484c7a33",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ True  True  True False False]\n",
      "[0 1 2]\n"
     ]
    }
   ],
   "source": [
    "cond = arr < 3\n",
    "\n",
    "print(cond)\n",
    "print(arr[cond])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63f9036c-4e52-4d61-9dd1-488d185b1ca0",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## 创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0a73300b-4895-4177-8c43-1472c0df203c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones(shape = 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "231da0ad-aa62-4a40-ae03-f369efb8388d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros(shape = 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c8f7c611-982f-48d7-a324-b444a08cc8a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3.14, 3.14, 3.14, 3.14, 3.14, 3.14])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.full(shape = 6, fill_value= 3.14)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c23429f5-6973-430e-89fd-8a2806faa66f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 5, 9, 0, 0, 8, 9, 7, 7, 0])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(0, 10, size = 10) # (low, high, size), 左开右闭"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5a120218-1a13-4122-be2a-8f4b74727644",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.22180966, -0.81845897, -2.04815986])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 正态分布\n",
    "np.random.randn(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "6ad93f69-4f93-402c-aa64-04e06d05c023",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0. ,  2.5,  5. ,  7.5, 10. ])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 等差数列，基本参数为 (start, stop, num)，间隔为 (stop-start)/(num-1)\n",
    "np.linspace(0, 10, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4be031f4-8ac2-46d8-934f-63c4c87a2ed0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([   1.,    2.,    4.,    8.,   16.,   32.,   64.,  128.,  256.,\n",
       "        512., 1024.])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 关闭科学计数法\n",
    "np.set_printoptions(suppress=True)\n",
    "# 等比数列, (start, stop, num)\n",
    "np.logspace(0, 10, base = 2, num = 11) # [ base^start, ..., base^end ], 一共 num 个元素"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f80d6e1f-e555-45f8-96b7-86533d8e1f87",
   "metadata": {},
   "source": [
    "*二维数组*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "84f8c028-e0f6-4e33-9f29-bbd3980ebcbf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros(shape = (3, 4))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e12c75d5-7bf6-4054-aacc-3fa351f5c914",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## 查看数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "93f5004a-a8f2-4781-9b8e-7af6c5725e39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 1, 1],\n",
       "       [3, 2, 1]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randint(0, 3, size = (2, 3)) + 1\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "9b2632b6-f127-411c-90a7-bf4755f3530d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3c69aac0-f898-4193-86df-fca3ba0905f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e7455b4b-19a4-4fb1-a68d-6f0545c8d357",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "f13f4156-602e-4fe5-b4ee-125883abd2ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据类型\n",
    "arr.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "38cbfc2a-17ea-4636-b319-0e31d68e8e64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 每个元素占用的大小，单位为字节\n",
    "arr.itemsize"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a3387f5-a322-4088-9b19-afe293690566",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## 保存和加载数组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03235886-b3e2-4cdd-8aff-32e2bf5229e3",
   "metadata": {},
   "source": [
    "**保存多个numpy数组，然后分别加载它们**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "6a5011e0-0513-43f0-a2a1-9b4563688e20",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.ones(shape = (3, 4))\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "0063a887-e859-4dce-8a69-e17cb25b425a",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.save('./data1', arr1) # 将 arr1 数组保存到 data1.npy 文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "971157fc-3545-4f61-9afa-f66b54b7601b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 加载 .npy 文件\n",
    "np.load('./data1.npy')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c66e4294-2191-43d7-a4d8-a38f4086fb77",
   "metadata": {},
   "source": [
    "**保存多个numpy数组，然后分别加载它们**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "c057cf4c-9bad-4ac9-8fb9-554474803841",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0.])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 = np.zeros(shape = 4)\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "77b68abe-afde-4ea2-a86a-934b331721cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savez('./data2', arr1=arr1, arr2=arr2) \n",
    "# 使用 savez 方法，第一个参数为文件名，无需指定扩展\n",
    "#      之后的参数是 数组key = 数组，数组key 可以自定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "12968e38-8743-48fb-9a51-8d79ecb29e04",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.load('./data2.npz')['arr1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "5d294c05-1ea5-4ec5-9241-6e9ec425c9cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0.])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.load('./data2.npz')['arr2']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a4006f9-98cd-4ac6-90de-ba70e4c53f61",
   "metadata": {},
   "source": [
    "**使用 csv 格式持久化数组**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "78a951f5-12ac-495d-8057-8b0254ad9f78",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 0, 5, 5, 0, 4, 9, 9, 5, 6])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.random.randint(0, 10, size=10)\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "a75fdfca-3e50-460f-b957-d687ef858388",
   "metadata": {},
   "outputs": [],
   "source": [
    "#指定后缀为 csv\n",
    "np.savetxt('./arr.csv', arr1, delimiter=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "74f61d96-98c7-4dc8-8ba9-d6b3d784417c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8., 0., 5., 5., 0., 4., 9., 9., 5., 6.])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.loadtxt('./arr.csv', delimiter=',')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae1d5d15-324d-4d2d-b792-2b7c021f685c",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## 数据类型与转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "4a487000-e115-4c9b-8627-cb4ec4d24238",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3], dtype=uint8)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#np.array([1,2,3], dtype=np.uint8)\n",
    "arr1 = np.array([1,2,3], dtype='uint8')\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "e2b1a48b-1f98-42f2-88ff-085f77f31b46",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-32768, -32767, -32766, ...,  32766,  32767, -32768], dtype=int16)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(-32768, 32769, dtype = np.int16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "ad393a25-de9f-488d-8288-439fd40f611f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-128, -127, -126, -125, -124, -123, -122, -121, -120, -119, -118,\n",
       "       -117, -116, -115, -114, -113, -112, -111, -110, -109, -108, -107,\n",
       "       -106, -105, -104, -103, -102, -101, -100,  -99,  -98,  -97,  -96,\n",
       "        -95,  -94,  -93,  -92,  -91,  -90,  -89,  -88,  -87,  -86,  -85,\n",
       "        -84,  -83,  -82,  -81,  -80,  -79,  -78,  -77,  -76,  -75,  -74,\n",
       "        -73,  -72,  -71,  -70,  -69,  -68,  -67,  -66,  -65,  -64,  -63,\n",
       "        -62,  -61,  -60,  -59,  -58,  -57,  -56,  -55,  -54,  -53,  -52,\n",
       "        -51,  -50,  -49,  -48,  -47,  -46,  -45,  -44,  -43,  -42,  -41,\n",
       "        -40,  -39,  -38,  -37,  -36,  -35,  -34,  -33,  -32,  -31,  -30,\n",
       "        -29,  -28,  -27,  -26,  -25,  -24,  -23,  -22,  -21,  -20,  -19,\n",
       "        -18,  -17,  -16,  -15,  -14,  -13,  -12,  -11,  -10,   -9,   -8,\n",
       "         -7,   -6,   -5,   -4,   -3,   -2,   -1,    0,    1,    2,    3,\n",
       "          4,    5,    6,    7,    8,    9,   10,   11,   12,   13,   14,\n",
       "         15,   16,   17,   18,   19,   20,   21,   22,   23,   24,   25,\n",
       "         26,   27,   28,   29,   30,   31,   32,   33,   34,   35,   36,\n",
       "         37,   38,   39,   40,   41,   42,   43,   44,   45,   46,   47,\n",
       "         48,   49,   50,   51,   52,   53,   54,   55,   56,   57,   58,\n",
       "         59,   60,   61,   62,   63,   64,   65,   66,   67,   68,   69,\n",
       "         70,   71,   72,   73,   74,   75,   76,   77,   78,   79,   80,\n",
       "         81,   82,   83,   84,   85,   86,   87,   88,   89,   90,   91,\n",
       "         92,   93,   94,   95,   96,   97,   98,   99,  100,  101,  102,\n",
       "        103,  104,  105,  106,  107,  108,  109,  110,  111,  112,  113,\n",
       "        114,  115,  116,  117,  118,  119,  120,  121,  122,  123,  124,\n",
       "        125,  126,  127, -128, -127], dtype=int8)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(-128, 130, dtype = np.int8)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34f8841e-1d62-4a9d-b6ec-15aed4fa76a7",
   "metadata": {},
   "source": [
    "**类型转换**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "1e68c6d8-c4ae-4c22-8873-f76a426e97d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3], dtype=int8)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 = np.asarray(arr1, dtype = 'int8')\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "1ed41907-cd4f-4d10-a25f-4ae9563b3c63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3], dtype=int16)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3 = arr1.astype('int16')\n",
    "arr3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af8e8534-419d-4083-b4af-1143d2a20a28",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## 数组运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "85ab306c-fa83-409e-ade7-a13e451f1f26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 9, 7, 1, 4],\n",
       "       [0, 0, 5, 2, 3],\n",
       "       [0, 6, 6, 7, 7]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd = np.random.randint(0, 10, size=(3,5))\n",
    "nd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "bff52d5b-a04a-4166-99d0-c47c2f08cfc3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[13, 14, 12,  6,  9],\n",
       "       [ 5,  5, 10,  7,  8],\n",
       "       [ 5, 11, 11, 12, 12]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd + 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "0cd55dd0-09f9-4256-a30d-0f7446d98c9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5,  6,  4, -2,  1],\n",
       "       [-3, -3,  2, -1,  0],\n",
       "       [-3,  3,  3,  4,  4]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd - 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "b2eed828-56a4-48a6-ab42-02cc4c252c5f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[16, 18, 14,  2,  8],\n",
       "       [ 0,  0, 10,  4,  6],\n",
       "       [ 0, 12, 12, 14, 14]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "404ad3ea-a0a9-4adc-851d-10845c11b74b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4. , 4.5, 3.5, 0.5, 2. ],\n",
       "       [0. , 0. , 2.5, 1. , 1.5],\n",
       "       [0. , 3. , 3. , 3.5, 3.5]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd / 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "6f68ab63-d7b4-4b85-bc85-867f923090fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 4, 3, 0, 2],\n",
       "       [0, 0, 2, 1, 1],\n",
       "       [0, 3, 3, 3, 3]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "5d238d4e-35ae-451b-92fd-c153b6deb581",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 0, 1, 1, 1],\n",
       "       [0, 0, 2, 2, 0],\n",
       "       [0, 0, 0, 1, 1]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd % 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "d02afdfb-ea39-47b5-b663-02b7b6a1df5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[64, 81, 49,  1, 16],\n",
       "       [ 0,  0, 25,  4,  9],\n",
       "       [ 0, 36, 36, 49, 49]])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd ** 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "4d8cdb39-bcff-4862-a34f-8dba12f59829",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[64, 81, 49,  1, 16],\n",
       "       [ 0,  0, 25,  4,  9],\n",
       "       [ 0, 36, 36, 49, 49]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.power(nd, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "69b34cd1-78e5-443e-9595-341106c3b594",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False, False,  True, False],\n",
       "       [ True,  True, False,  True,  True],\n",
       "       [ True, False, False, False, False]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd < 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "07a66383-4e9a-4211-8b3c-84897009bc6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False, False, False,  True],\n",
       "       [False, False, False, False, False],\n",
       "       [False, False, False, False, False]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd == 4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be35b4be-2d58-45da-8ce2-7b3f04d222b4",
   "metadata": {},
   "source": [
    "赋值运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "8294796d-67c8-417e-afc9-c50744f11428",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 9 10  8  2  5]\n",
      " [ 1  1  6  3  4]\n",
      " [ 1  7  7  8  8]]\n"
     ]
    }
   ],
   "source": [
    "nd = nd + 1\n",
    "print(nd)\n",
    "nd = nd - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "cc823e4e-2f9e-4640-a3ac-24de46037874",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 9 10  8  2  5]\n",
      " [ 1  1  6  3  4]\n",
      " [ 1  7  7  8  8]]\n"
     ]
    }
   ],
   "source": [
    "nd += 1\n",
    "print(nd)\n",
    "nd -= 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "227a2ad3-50b2-48e0-b29d-387393d03f76",
   "metadata": {},
   "outputs": [],
   "source": [
    "#不支持\n",
    "#nd /= 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "53026f01-e5ce-45d6-83d6-ab46c9d9f21f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 3, 2, 0, 1],\n",
       "       [0, 0, 1, 0, 1],\n",
       "       [0, 2, 2, 2, 2]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 支持\n",
    "nd //= 3\n",
    "nd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "8aad425d-fda5-4c3f-977a-3b8992e6269c",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/bx/3v01n90d57v4nfksc48wtw0w0000gn/T/ipykernel_6007/2935957506.py:1: RuntimeWarning: divide by zero encountered in divide\n",
      "  1/nd\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[0.5       , 0.33333333, 0.5       ,        inf, 1.        ],\n",
       "       [       inf,        inf, 1.        ,        inf, 1.        ],\n",
       "       [       inf, 0.5       , 0.5       , 0.5       , 0.5       ]])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1/nd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33b9a24e-b831-4032-aede-01cde0faed68",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## 复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "92f1d255-ec1b-42ea-af82-016f870eaec3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def compare(a, b):\n",
    "    print('b 和 a 对应同一个内存地址？', b is a)\n",
    "    print('b 的根数据和 a 一样？', b.base is a)\n",
    "    print('b 的数据是自己的？', b.flags.owndata)\n",
    "    print('a 的数据是自己的？', a.flags.owndata)\n",
    "    b[0,0] = 1024\n",
    "    display(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "620bd4ac-41d1-44dc-a14b-929358d7b719",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b 和 a 对应同一个内存地址？ True\n",
      "b 的根数据和 a 一样？ False\n",
      "b 的数据是自己的？ True\n",
      "a 的数据是自己的？ True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1024,    4,    8,    9,    3],\n",
       "       [   1,    4,    2,    9,    5],\n",
       "       [   6,    4,    5,    0,    3],\n",
       "       [   6,    8,    4,    2,    9]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1024,    4,    8,    9,    3],\n",
       "       [   1,    4,    2,    9,    5],\n",
       "       [   6,    4,    5,    0,    3],\n",
       "       [   6,    8,    4,    2,    9]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.random.randint(0, 10, size = (4,5))\n",
    "compare(a, a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "00851412-ed17-494a-828a-b89bc393b1d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b 和 a 对应同一个内存地址？ False\n",
      "b 的根数据和 a 一样？ True\n",
      "b 的数据是自己的？ False\n",
      "a 的数据是自己的？ True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1024,    8,    2,    1,    5],\n",
       "       [   6,    1,    7,    2,    6],\n",
       "       [   1,    9,    8,    8,    4],\n",
       "       [   7,    7,    1,    9,    5]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1024,    8,    2,    1,    5],\n",
       "       [   6,    1,    7,    2,    6],\n",
       "       [   1,    9,    8,    8,    4],\n",
       "       [   7,    7,    1,    9,    5]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.random.randint(0, 10, size = (4,5))\n",
    "compare(a, a.view())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "5c134f80-3aac-40ee-875a-656b1e3e11c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b 和 a 对应同一个内存地址？ False\n",
      "b 的根数据和 a 一样？ False\n",
      "b 的数据是自己的？ True\n",
      "a 的数据是自己的？ True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[7, 1, 3, 7, 8],\n",
       "       [4, 4, 6, 7, 8],\n",
       "       [6, 2, 4, 4, 4],\n",
       "       [0, 0, 9, 1, 8]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1024,    1,    3,    7,    8],\n",
       "       [   4,    4,    6,    7,    8],\n",
       "       [   6,    2,    4,    4,    4],\n",
       "       [   0,    0,    9,    1,    8]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.random.randint(0, 10, size = (4,5))\n",
    "compare(a, a.copy())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0c04578-715b-4c74-b521-77a2fa1fed13",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## 索引、切片和迭代"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38fe4f8a-4f03-4944-9d60-c4dd6f435e06",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### 花式索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "c1cfac6d-38db-4713-9d4c-a0d21ffcfc8f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 3, 4, 4, 8, 1, 1, 0, 1, 9, 5, 7, 1, 1, 8, 4, 4, 3, 1, 1])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randint(0, 10, 20)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "edc8ef84-8328-43bd-acc3-b6881a8c2f0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 4, 1])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过花式索引获取元素，将元素复制到新数组\n",
    "arr2 = arr[[1,3,5]]\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "559fcad5-71e0-4002-bd97-0db5682774e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 修改新数组，不会改变源数组\n",
    "arr2[0] = -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "d5918810-5001-41bd-802b-9a8c3efae56c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 3, 4, 4, 8, 1, 1, 0, 1, 9, 5, 7, 1, 1, 8, 4, 4, 3, 1, 1])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8028b365-5756-4941-bad2-15771c9aed8c",
   "metadata": {},
   "source": [
    "**二维**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "67f1afe4-729f-46cc-a702-ad3b4632773e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 6, 3, 0, 5],\n",
       "       [7, 8, 2, 4, 8],\n",
       "       [1, 5, 3, 7, 2],\n",
       "       [4, 3, 4, 0, 7]])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3 = np.random.randint(0, 10, size = (4, 5))\n",
    "arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "76c076ef-3c4d-4b6a-8b3a-a0271cca5a87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 8, 2, 4, 8],\n",
       "       [4, 3, 4, 0, 7]])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#取出第 2、4 行\n",
    "arr3[[1,3]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "dca8f37c-0710-4969-b4e3-f3cf699d0184",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 7])"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#取出 arr3[1,2], arr3[3,4] 两个元素。注意：不是交点处的元素\n",
    "arr3[[1,3], [2, 4]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "5d4ab178-2781-48cd-bd12-f4649fd16050",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 4, 0, 7])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 四个元素，坐标分别为 1,1 3,2 3,3 3,4\n",
    "arr3[[1, 3, 3, 3], [1, 2, 3, 4]]\n",
    "\n",
    "#扩展\n",
    "#arr3[[1, 3, 3, 3], [1]]\n",
    "# 1,1 3,1 3,1 3,1 四个元素，即 array([3, 6, 6, 6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "f50612b2-8f34-4d6e-838e-f50e25453cbd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[4, 8]],\n",
       "\n",
       "       [[0, 7]]])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#取出两行和两列交点处的四个元素\n",
    "arr3[[1,3]][:, [[3,4]]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d21cab19-b496-48e6-8bd8-e393be2b1020",
   "metadata": {},
   "source": [
    "**元素为布尔类型的花式索引**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "a07188e0-4e6d-4936-a779-04d76c0b6900",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([53, 44, 76, 67, 76, 50, 84, 87, 62, 70,  0, 87, 78, 83, 36, 74, 84,\n",
       "       43, 59,  2])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr4 = np.random.randint(0, 100, 20)\n",
    "arr4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "0ca69754-cad2-40b0-b6ff-80c7af9ba995",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True, False,  True,  True,  True, False,  True,  True,  True,\n",
       "        True, False,  True,  True,  True, False,  True,  True, False,\n",
       "        True, False])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond = arr4 > 50\n",
    "cond"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "ed98dffb-8dbb-463b-8f47-4f82591d0338",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([53, 76, 67, 76, 84, 87, 62, 70, 87, 78, 83, 74, 84, 59])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr4[cond]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "66fe531b-cc68-4f2a-8580-5ec48359eda4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([84, 87,  0, 87, 83, 84,  2])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond1 = arr4 < 20\n",
    "cond2 = arr4 > 80\n",
    "cond3 = cond1 | cond2\n",
    "arr4[cond3]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a4a0a21-1fb4-485f-94b2-c7177aa7031f",
   "metadata": {},
   "source": [
    "补充：**argsort**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "c3497297-0c17-4290-abe5-5916a02d1713",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([45,  0,  3, 39, 27])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr5 = np.random.randint(0, 50, size=5)\n",
    "arr5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "e61b1ea6-10dc-44e2-9ee0-29abf785e739",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  3, 27, 39, 45])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sort(arr5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "ceb34a43-6332-4d4d-87a5-d059b2d7fd85",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 4, 3, 0])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sort_idx = arr5.argsort()\n",
    "sort_idx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "59ffe199-656b-485a-8a8f-6a5746a75b42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  3, 27, 39, 45])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr5[sort_idx]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f07911a7-e7ea-41c8-a18f-b4fa072d3fc8",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### 一维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "805b47f0-c952-49db-b12c-8cf4ccc99e9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([66, 25, 56, 42, 93, 47, 18, 47, 55, 43])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.random.randint(0, 100, size = 10)\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "d882b696-4cdc-458a-a49a-633a643ae909",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "66"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "4c216ca1-a2bf-4f43-a77c-f867ef70dc83",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "43"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "51044654-f52e-4522-bd63-04d5b44042d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([66, 56, 55])"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#一次取多个\n",
    "arr1[[0,2,8]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1121ea19-bc8e-4745-a04b-fe09f8e5a376",
   "metadata": {},
   "source": [
    "**切片**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "741bf37a-6964-43cb-8418-1cf27a8221e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([42, 93, 47])"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1[3:6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "d0d91301-4f63-4448-a614-42245bdc7b17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True])"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#指定步长，arr1[3:6:2] 相当于 arr1[[3,5]]\n",
    "arr1[3:6:2] == arr1[[3,5]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "493a0325-3075-4bd4-ac23-dd74bda95301",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([43, 55, 47, 18, 47, 93, 42, 56, 25, 66])"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#从后向前取，步长为-1\n",
    "arr1[::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8717714-5b7d-44b2-b234-dfdc1119079b",
   "metadata": {},
   "source": [
    "### 二维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "03a76e4c-ae3d-4070-b238-7f20d5263dc9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[84, 42,  5, 70, 37],\n",
       "       [83, 74, 51, 34,  8],\n",
       "       [84, 51, 40, 43, 82]])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 = np.random.randint(0, 100, size = (3, 5))\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "dee54478-91a9-4d1b-ae0b-304491ba3811",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "74"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2[1,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "1a65cfe8-65e8-4cc2-b89b-ee382568e5e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "82"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2[-1,-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "d2368465-fad7-4041-a240-0a665eac7deb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[84, 42,  5, 70, 37],\n",
       "       [84, 51, 40, 43, 82]])"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#取指定多行\n",
    "arr2[[0, 2]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "8601c7de-b6b4-4320-b2c7-e0d288e682a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[84,  5, 37],\n",
       "       [83, 51,  8],\n",
       "       [84, 40, 82]])"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#取指定多列\n",
    "arr2[:, [0,2,4]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "81c98efb-053b-4935-a8e9-df639cef400e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([83, 74, 51, 34,  8])"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第二行\n",
    "arr2[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "5d37d436-782a-4c7d-9b6a-aa9442245842",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([42, 74, 51])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第二列\n",
    "arr2[:, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "78ba0fef-ac5c-4d29-8e77-908c6f7e6553",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[83, 74, 51, 34,  8],\n",
       "       [84, 51, 40, 43, 82]])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#通过切片取某几行\n",
    "arr2[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "39ff70eb-32a1-4362-85ec-34ee56d76761",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[70, 37],\n",
       "       [34,  8],\n",
       "       [43, 82]])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#通过切片取某几列\n",
    "arr2[:, 3:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "8250fe83-d87d-4cb4-b15e-9d5f4e24ed94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([51, 34,  8])"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取第二行后三个数\n",
    "arr2[1, -3:]\n",
    "# 等价于\n",
    "# arr2[1, 2:]\n",
    "# arr2[1, [2, 3, 4]]\n",
    "# arr2[1, [-3, -2, -1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "fea26aeb-c90e-4210-a935-9f1e19d01bfe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[34,  8],\n",
       "       [43, 82]])"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用切片取出右下角 2x2 区域\n",
    "arr2[-2:, -2:]\n",
    "# 等价于\n",
    "# arr2[1:, 3:]\n",
    "# arr2[[1,2], 3:]\n",
    "# arr2[1:, [3,4]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "eaa23853-eb8f-4d7f-99fd-a811c267cad8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[42, 70],\n",
       "       [74, 34],\n",
       "       [51, 43]])"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#通过切片指定范围和步长\n",
    "arr2[:, 1::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "74437d39-edb3-4855-9185-6887649d6a3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  84,   42,    5,   70,   37],\n",
       "       [  83,   74,   51,   34,    8],\n",
       "       [  84,   51, 1024,   43, 1024]])"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#赋值\n",
    "arr2[2, [-3, -1]] = 1024\n",
    "arr2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1e917c8-71b8-4425-834b-3b214d6df1f6",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## 形状改变"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d21b8017-861c-4d3c-b28e-4c4ff1e4316e",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### reshape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "f687680d-e750-495d-bcd5-1fc7bf12ab94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[44, 70, 32, 11],\n",
       "       [ 6, 51, 43, 64],\n",
       "       [12, 13, 53, 64]])"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randint(0, 100, size = (3, 4))\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "822e4ca0-a622-4edc-9f68-fd7495a73b28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4)"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "191d04c4-29d1-49cd-a73c-3b6e2e2efdc4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[44, 70, 32],\n",
       "       [11,  6, 51],\n",
       "       [43, 64, 12],\n",
       "       [13, 53, 64]])"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.reshape(4,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "d6a81f40-d760-4251-acf0-59a8c9935b92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[44, 70, 32, 11,  6, 51],\n",
       "       [43, 64, 12, 13, 53, 64]])"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.reshape(2, 6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "086c28c4-cf21-4578-b312-0c9d80a67d33",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[44, 70, 32, 11,  6, 51],\n",
       "       [43, 64, 12, 13, 53, 64]])"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# -1 表示最后自动计算\n",
    "arr.reshape(2, -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "384c0ae1-9517-4703-a80c-b1198bdb5384",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[44, 70],\n",
       "       [32, 11],\n",
       "       [ 6, 51],\n",
       "       [43, 64],\n",
       "       [12, 13],\n",
       "       [53, 64]])"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.reshape(-1, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "e4930d8f-ca24-45c4-9b62-27a42c0e62b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([44, 70, 32, 11,  6, 51, 43, 64, 12, 13, 53, 64])"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 只指定 -1 会转为一维平铺数组\n",
    "arr.reshape(-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f088884e-bede-4f28-8caa-a48c9055cdc8",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### 叠加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "dafbc5ec-315f-48bb-b77c-bc36515ac6cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 4, 6, 1],\n",
       "       [8, 5, 7, 7]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0, -2, -2, -4],\n",
       "       [ 1, -4,  1, -4],\n",
       "       [ 3,  4, -2, -2]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr1 = np.random.randint(0, 10, size=(2, 4))\n",
    "arr2 = np.random.randint(-5, 5, size=(3, 4))\n",
    "\n",
    "display(arr1, arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "f663e72a-e755-4150-b877-fb181c4e65a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  4,  6,  1],\n",
       "       [ 8,  5,  7,  7],\n",
       "       [ 0, -2, -2, -4],\n",
       "       [ 1, -4,  1, -4],\n",
       "       [ 3,  4, -2, -2],\n",
       "       [ 4,  4,  6,  1],\n",
       "       [ 8,  5,  7,  7]])"
      ]
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组合并，默认进行行合并(axis=0)，需要列数相同，否则报错\n",
    "np.concatenate([arr1, arr2, arr1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3dacefa-ca00-47e1-9f14-11da8016c8d6",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "id": "fbe62920-1c8d-4c1e-9f61-5f3f5d978efb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 7, 8, 1, 0],\n",
       "       [5, 6, 2, 4, 9],\n",
       "       [3, 0, 7, 9, 7]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[-3,  3,  0, -5],\n",
       "       [-4,  3, -5, -3],\n",
       "       [ 2, -1, -2, -3]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr1 = np.random.randint(0, 10, size=(3, 5))\n",
    "arr2 = np.random.randint(-5, 5, size=(3, 4))\n",
    "\n",
    "display(arr1, arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "id": "91f73c87-bd3b-4740-a02a-1791abc79578",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  7,  8,  1,  0, -3,  3,  0, -5],\n",
       "       [ 5,  6,  2,  4,  9, -4,  3, -5, -3],\n",
       "       [ 3,  0,  7,  9,  7,  2, -1, -2, -3]])"
      ]
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 指定 axis=1/axis=-1 表示列合并，需要行数相同，否则报错\n",
    "np.concatenate([arr1, arr2], axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af51f1c3-c487-4d76-b84e-91a0464b79d6",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### 拆分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "id": "48901a86-65d6-4568-a683-12fc9e18bed9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[83, 75, 41, 24, 65, 13, 28, 87, 83],\n",
       "       [83, 85, 47, 72, 93,  7, 61, 90, 60],\n",
       "       [92, 77, 50, 83, 66, 18, 77, 47, 15],\n",
       "       [97, 92, 57,  9, 76, 46, 58, 30, 35],\n",
       "       [57, 25, 76,  3, 96, 42,  6, 60, 22],\n",
       "       [74, 76,  8,  9, 92, 38, 80, 95, 10]])"
      ]
     },
     "execution_count": 170,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.random.randint(0, 100, size = (6,9))\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "id": "9928e5ab-ff9a-4b1a-b896-e9eca4d60572",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[83, 75, 41, 24, 65, 13, 28, 87, 83],\n",
       "        [83, 85, 47, 72, 93,  7, 61, 90, 60],\n",
       "        [92, 77, 50, 83, 66, 18, 77, 47, 15]]),\n",
       " array([[97, 92, 57,  9, 76, 46, 58, 30, 35],\n",
       "        [57, 25, 76,  3, 96, 42,  6, 60, 22],\n",
       "        [74, 76,  8,  9, 92, 38, 80, 95, 10]])]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[array([[83, 75, 41, 24, 65, 13, 28, 87, 83],\n",
       "        [83, 85, 47, 72, 93,  7, 61, 90, 60]]),\n",
       " array([[92, 77, 50, 83, 66, 18, 77, 47, 15],\n",
       "        [97, 92, 57,  9, 76, 46, 58, 30, 35]]),\n",
       " array([[57, 25, 76,  3, 96, 42,  6, 60, 22],\n",
       "        [74, 76,  8,  9, 92, 38, 80, 95, 10]])]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[array([[83, 75, 41, 24, 65, 13, 28, 87, 83]]),\n",
       " array([[83, 85, 47, 72, 93,  7, 61, 90, 60],\n",
       "        [92, 77, 50, 83, 66, 18, 77, 47, 15]]),\n",
       " array([[97, 92, 57,  9, 76, 46, 58, 30, 35],\n",
       "        [57, 25, 76,  3, 96, 42,  6, 60, 22]]),\n",
       " array([[74, 76,  8,  9, 92, 38, 80, 95, 10]])]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#按行拆分\n",
    "\n",
    "#平均拆成两部分\n",
    "display(np.split(arr1, 2))\n",
    "print()\n",
    "\n",
    "#平均拆成三部分\n",
    "display(np.split(arr1, 3))\n",
    "print()\n",
    "\n",
    "#指定拆分点。[1,3,5] -> [:1],[1:3],[3:5],[5:]\n",
    "display(np.split(arr1, [1,3,5]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "id": "1a08766c-d6ce-4fe4-9bcb-710ef648f9a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[83, 75, 41],\n",
       "        [83, 85, 47],\n",
       "        [92, 77, 50],\n",
       "        [97, 92, 57],\n",
       "        [57, 25, 76],\n",
       "        [74, 76,  8]]),\n",
       " array([[24, 65, 13],\n",
       "        [72, 93,  7],\n",
       "        [83, 66, 18],\n",
       "        [ 9, 76, 46],\n",
       "        [ 3, 96, 42],\n",
       "        [ 9, 92, 38]]),\n",
       " array([[28, 87, 83],\n",
       "        [61, 90, 60],\n",
       "        [77, 47, 15],\n",
       "        [58, 30, 35],\n",
       "        [ 6, 60, 22],\n",
       "        [80, 95, 10]])]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 同理，按列拆分，需指定轴为 1\n",
    "\n",
    "display(np.split(arr1, 3, axis=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "157129a1-c708-45f6-a6b5-23d873fb5fef",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### 转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "id": "82654324-55a2-4e8c-88df-d24a584868d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 8, 4, 3, 1],\n",
       "       [4, 1, 8, 1, 7],\n",
       "       [0, 8, 2, 2, 8]])"
      ]
     },
     "execution_count": 173,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randint(0, 10, size=(3,5))\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "id": "4ffd95af-ed9c-4425-9647-d3889e19b0f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 8, 4],\n",
       "       [3, 1, 4],\n",
       "       [1, 8, 1],\n",
       "       [7, 0, 8],\n",
       "       [2, 2, 8]])"
      ]
     },
     "execution_count": 174,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.reshape(5,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "id": "c77a48c5-c91c-4769-9dfa-411c00d7a87d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 4, 0],\n",
       "       [8, 1, 8],\n",
       "       [4, 8, 2],\n",
       "       [3, 1, 2],\n",
       "       [1, 7, 8]])"
      ]
     },
     "execution_count": 178,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转置（行变列、列变行）\n",
    "arr.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "id": "f4816af4-4457-4b3e-bbe6-98489de6fb60",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 4, 0],\n",
       "       [8, 1, 8],\n",
       "       [4, 8, 2],\n",
       "       [3, 1, 2],\n",
       "       [1, 7, 8]])"
      ]
     },
     "execution_count": 181,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.transpose(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1fda323-483d-4f0f-b282-858bc8788bf7",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## 广播"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44e903ac-b6ea-4597-9c26-91ce46621b46",
   "metadata": {},
   "source": [
    "**示例 1**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "id": "c9c20fbc-24ff-4b70-84d6-c8f84cbfc636",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 6, 4],\n",
       "       [5, 0, 3],\n",
       "       [6, 8, 9],\n",
       "       [0, 5, 4],\n",
       "       [7, 7, 0]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr1 = np.random.randint(0, 10, size = (5,3))\n",
    "\n",
    "arr2 = np.arange(1, 4) # 创建数组 [1,2,3]\n",
    "\n",
    "display(arr1, arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "id": "0e0f40fa-264b-4ec6-8b0f-76942f62df26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10,  8,  7],\n",
       "       [ 6,  2,  6],\n",
       "       [ 7, 10, 12],\n",
       "       [ 1,  7,  7],\n",
       "       [ 8,  9,  3]])"
      ]
     },
     "execution_count": 184,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# arr2(1, 4) ===> arr2(5, 4)\n",
    "arr1 + arr2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b4d758f-01b5-4801-9cbc-bc3b8b20b065",
   "metadata": {},
   "source": [
    "**示例 2**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "id": "20a98d7e-d31c-474d-acd0-36c9adf829d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 6, 0, 5, 7],\n",
       "       [7, 8, 2, 0, 7],\n",
       "       [9, 3, 4, 4, 0],\n",
       "       [2, 3, 7, 3, 3]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([26, 20, 13, 12, 17])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[0.30769231, 0.3       , 0.        , 0.41666667, 0.41176471],\n",
       "       [0.26923077, 0.4       , 0.15384615, 0.        , 0.41176471],\n",
       "       [0.34615385, 0.15      , 0.30769231, 0.33333333, 0.        ],\n",
       "       [0.07692308, 0.15      , 0.53846154, 0.25      , 0.17647059]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr1 = np.random.randint(0, 10, size = (4, 5))\n",
    "display(arr1)\n",
    "\n",
    "# 计算每一列的和\n",
    "arr2 = arr1.sum(axis = 0)\n",
    "display(arr2)\n",
    "\n",
    "# 计算每一项占当前列的百分比\n",
    "display(arr1 / arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aebe9a2d-77d8-4238-8d22-1181dd55d545",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## 通用函数"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "9016ba9a-5e21-482d-aecb-1f2584e4909f",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### 一般函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 242,
   "id": "cf109242-8a86-4863-a6e8-ae3ce9d01389",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.141592653589793"
      ]
     },
     "execution_count": 242,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.pi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "id": "d5db6c82-e467-45a2-b24c-77da1280d2fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 199,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sin(np.pi / 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "id": "59180587-b3e0-4bd3-90d9-69326205f0cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 200,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cos(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b83ed5bb-dfd7-4fe4-94b6-c54f73657f94",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 202,
   "id": "53c8b6a2-d40f-4ab1-8be2-dd6d556cb5c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32.0"
      ]
     },
     "execution_count": 202,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(1024)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 204,
   "id": "82665550-bf1e-4a02-b1c8-dbd1f72620e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1024"
      ]
     },
     "execution_count": 204,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.square(32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 205,
   "id": "4b4b2119-6117-475a-8d20-f4cba29eb5a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "27"
      ]
     },
     "execution_count": 205,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.power(3, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 206,
   "id": "7ac10ea4-20bb-4132-ad68-4174109d1282",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 206,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 ** 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 212,
   "id": "daf29377-eb85-4025-b118-f24fdce682b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 212,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "8 ** (1/3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 213,
   "id": "1bf0fb00-ef72-46b5-a9e4-0393fe9c12b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 213,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.log10(100)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5144f185-51b5-473e-bd9b-936e09b06c7b",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "id": "5bb7b7ff-ea3c-414e-bd61-144667cd0ae9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.0"
      ]
     },
     "execution_count": 221,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ceil(3.99)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 223,
   "id": "fa1198df-5476-4530-aeff-e4469a99f7b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 223,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.floor(3.99)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 226,
   "id": "327aec96-647a-4b57-9d22-62f9487e9914",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 226,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.round(3.10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 235,
   "id": "7ed872ac-9923-4569-b56b-40bdd9bc7731",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.4"
      ]
     },
     "execution_count": 235,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.round(3.45, decimals=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d26de58-032f-4813-8a2e-3f298a286746",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 218,
   "id": "853d50a8-6528-4b85-9697-2d2398d7b0cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 4, 3, 4, 5])"
      ]
     },
     "execution_count": 218,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([1,2,3,4,5])\n",
    "y = np.array([5,4,3,2,1])\n",
    "\n",
    "# 每一列的最大值\n",
    "np.maximum(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 219,
   "id": "d3a33969-afa3-4ca8-9f92-aaf5d557ab75",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 2, 1])"
      ]
     },
     "execution_count": 219,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.minimum(x, y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd6a3235-068e-4a29-ba4a-cbc47aced0d7",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 220,
   "id": "8181dc75-6584-41bd-8e2a-fd132cef6a79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11, 17])"
      ]
     },
     "execution_count": 220,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = np.array([[3,4], [5,6]])\n",
    "i = np.array([1,2])\n",
    "\n",
    "# 计算一维数组向量内积\n",
    "#  11 = 3*1 + 4*2\n",
    "#  17 = 5*1 + 6*2\n",
    "np.inner(i, z)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b7d08e0-05f0-4de2-a389-1e91e47a67dc",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 240,
   "id": "99427b55-8704-44ae-a2c5-4180b19714d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([21, 34,  0, 10,  8, 42, 43, 39, 49,  9, 46, 41,  0, 46,  9, 21, 25,\n",
       "       33, 28, 45])"
      ]
     },
     "execution_count": 240,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randint(0, 50, size = 20)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 241,
   "id": "b827a66a-d518-4050-9427-35db99160978",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([21, 34, 10, 10, 10, 40, 40, 39, 40, 10, 40, 40, 10, 40, 10, 21, 25,\n",
       "       33, 28, 40])"
      ]
     },
     "execution_count": 241,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#指定数组元素范围\n",
    "np.clip(arr, 10, 40)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aaacfd13-fe9c-4b73-b95e-cd64aea75a52",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### where"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 248,
   "id": "307aba3b-8a9f-4842-95da-5d4899fdccf4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 4, 1, 3, 9, 9, 1, 5, 5, 8])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([3, 6, 1, 8, 7, 8, 2, 7, 6, 9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([0, 6, 1, 8, 9, 8, 1, 7, 5, 9])"
      ]
     },
     "execution_count": 248,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1 = np.random.randint(0, 10, size=10)\n",
    "a2 = np.random.randint(0, 10, size=10)\n",
    "\n",
    "display(a1, a2)\n",
    "\n",
    "cond = np.array([True, False, True, False, True, False, True, False, True, False])\n",
    "\n",
    "# 根据 cond 进行选择，如果 cond[i] 为真则选择 a1[i]，反之选择 a2[i]\n",
    "np.where(cond, a1, a2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 260,
   "id": "baac4175-45aa-4463-a679-4dd81d5169c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 18,  26,  25,  11,  70,   8,  49,  90,  -2, 102])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([ 18,  26,  25,  11,  70,   8,  49,  90,   0, 100])"
      ]
     },
     "execution_count": 260,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a3 = np.random.randint(-5, 105, size=10)\n",
    "display(a3)\n",
    "\n",
    "# 如果 a3[i] < 5 则选择 a3[i] 反之 >=5 选择 5\n",
    "a4 = np.where(0 <= a3, a3, 0)\n",
    "a5 = np.where(a4 <= 100, a4, 100)\n",
    "a5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22bcf48a-ec1e-41d0-a077-6bcfeb6e6706",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### 集合运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 263,
   "id": "ec7509de-2a65-4231-920c-30151194f6c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([2, 4, 6, 8])\n",
    "B = np.array([3, 4, 7, 8])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 265,
   "id": "f146946e-9cd4-47d1-a7fc-b6c7a22d4723",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 8])"
      ]
     },
     "execution_count": 265,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交集\n",
    "np.intersect1d(A, B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 266,
   "id": "3378c0d6-7e7b-42e0-98c9-cf8dd4c77c04",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 4, 6, 7, 8])"
      ]
     },
     "execution_count": 266,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 并集\n",
    "np.union1d(A, B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 269,
   "id": "85ded596-4473-40ac-916f-b8cddc89e866",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 6])"
      ]
     },
     "execution_count": 269,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 差集 A-B：A中有的、B中没有的\n",
    "np.setdiff1d(A, B)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c8827d9-f49d-4de2-962a-aaaeef2decd1",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### 数学和统计函数\n",
    "\n",
    "- min 最小值\n",
    "- max 最大值\n",
    "- mean 平均值\n",
    "- median 中位数\n",
    "- sum 求和\n",
    "- std 标准差\n",
    "- var 方差\n",
    "- cumsum 累加和\n",
    "- cumprod\n",
    "- argmin 最小值索引\n",
    "- argmax 最大值索引\n",
    "- argwhere 符合条件元素的索引\n",
    "- cov 协方差矩阵\n",
    "- corrcoef 相关性系数\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 270,
   "id": "5c9b1f03-c444-494c-8ec7-43639c2f3fe1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 270,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1 = np.array([1, 7, 2, 19, 23, 0, 88, 11, 6, 11])\n",
    "\n",
    "a1.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 271,
   "id": "c328237d-bc1d-495f-aab2-795ea470bc6a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 271,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1.argmax()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 272,
   "id": "b3017bc5-f207-47fe-978f-24c139745557",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3],\n",
       "       [4],\n",
       "       [6],\n",
       "       [7],\n",
       "       [9]])"
      ]
     },
     "execution_count": 272,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argwhere(a1 > 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 275,
   "id": "bdf478ee-be8d-48a4-a704-e73fb8d2a628",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1,   8,  10,  29,  52,  52, 140, 151, 157, 168])"
      ]
     },
     "execution_count": 275,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# c(i) =  a1[i] + c(i-1)\n",
    "np.cumsum(a1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 277,
   "id": "a4da278d-7e47-4537-962c-46fec74be331",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 1, 6, 4, 4],\n",
       "       [7, 5, 6, 8, 2],\n",
       "       [5, 1, 7, 2, 7],\n",
       "       [0, 7, 5, 7, 4]])"
      ]
     },
     "execution_count": 277,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2 = np.random.randint(0, 10, size=(4,5))\n",
    "a2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 278,
   "id": "3ffb8670-8402-4f80-9c0e-de423e5ac566",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4.5 , 3.5 , 6.  , 5.25, 4.25])"
      ]
     },
     "execution_count": 278,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2.mean(axis = 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 279,
   "id": "7a7b12b9-72fc-4ae3-b67f-8bb6b8a1acbe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4.2, 5.6, 4.4, 4.6])"
      ]
     },
     "execution_count": 279,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2.mean(axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 281,
   "id": "0a98fbef-aae6-473f-a87f-d341a5ba272f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4.2 ,  1.35,  4.15, -3.9 ],\n",
       "       [ 1.35,  5.3 , -2.8 ,  0.05],\n",
       "       [ 4.15, -2.8 ,  7.8 , -4.3 ],\n",
       "       [-3.9 ,  0.05, -4.3 ,  8.3 ]])"
      ]
     },
     "execution_count": 281,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 协方差矩阵\n",
    "np.cov(a2, rowvar=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 282,
   "id": "be5ddc68-8cdc-4ab9-b22d-a94ef97f1053",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.        ,  0.28613513,  0.72506368, -0.66054273],\n",
       "       [ 0.28613513,  1.        , -0.43548459,  0.00753864],\n",
       "       [ 0.72506368, -0.43548459,  1.        , -0.53441927],\n",
       "       [-0.66054273,  0.00753864, -0.53441927,  1.        ]])"
      ]
     },
     "execution_count": 282,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 相关性系数\n",
    "np.corrcoef(a2, rowvar=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bca8d5eb-7df7-41b1-9c69-31a07dfa4998",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## 矩阵运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2f89fd3-f858-416c-85f2-51a9b4247793",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### 矩阵乘法\n",
    "\n",
    "也称为点乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 285,
   "id": "efd8c627-826f-4317-bbb2-2a19de5347df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 25,  23],\n",
       "       [ -4, -11]])"
      ]
     },
     "execution_count": 285,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array([[4,2,3],\n",
    "              [1,3,1]]) # 2x3\n",
    "B = np.array([[2,7],\n",
    "              [-5,-7],\n",
    "              [9,3]])   # 3x2\n",
    "\n",
    "# A 的最后一维要和 B 的第一维相同\n",
    "np.dot(A, B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 286,
   "id": "aa40d1c1-fba0-43e2-8168-c1022c91fcbc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 25,  23],\n",
       "       [ -4, -11]])"
      ]
     },
     "execution_count": 286,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A @ B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 287,
   "id": "9f2b33e9-2922-4495-af90-effcd71b0434",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 25,  23],\n",
       "       [ -4, -11]])"
      ]
     },
     "execution_count": 287,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.dot(B)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b9af317-c206-4138-9190-500585a462e3",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### 其他运算\n",
    "\n",
    "矩阵的逆、行列式、特征值、特征向量、qr分解值、svd分解值\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 298,
   "id": "d557284c-f457-4082-b1d3-b44c35339960",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 1, 1],\n",
       "       [5, 2, 6],\n",
       "       [8, 3, 1]])"
      ]
     },
     "execution_count": 298,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from numpy.linalg import inv, det\n",
    "A = np.random.randint(1, 10, size=(3,3))\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 299,
   "id": "47aa4a04-9140-40a4-b692-09610ef827b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2.66666667, -0.33333333, -0.66666667],\n",
       "       [-7.16666667,  0.83333333,  2.16666667],\n",
       "       [ 0.16666667,  0.16666667, -0.16666667]])"
      ]
     },
     "execution_count": 299,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.set_printoptions(suppress=True)\n",
    "\n",
    "# 计算 A 的逆矩阵\n",
    "B = inv(A)\n",
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 300,
   "id": "f72044a6-2a67-463d-91b6-a97bd367b91c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1., -0.,  0.],\n",
       "       [-0.,  1.,  0.],\n",
       "       [ 0.,  0.,  1.]])"
      ]
     },
     "execution_count": 300,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A @ B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 301,
   "id": "f503d49f-aae4-4ea9-8f47-c55337934812",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-6.0"
      ]
     },
     "execution_count": 301,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 行列式，https://en.wikipedia.org/wiki/Determinant\n",
    "det(A)\n",
    "# 3(2-18)+(48-5)+(15-16)=-48+43-1=-6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83c5570a-a64e-4dfe-9dfd-fd8443600fc6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
